package com.zui.util.excel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 创建并获取Excel的服务类
 * 
 */
public class ExcelServiceImpl implements ExcelService {

	/**
	 * 创建Excel
	 * 
	 * @param clazz
	 *            类名
	 * @param datas
	 *            数据
	 * @param title
	 *            标题
	 * @return
	 */
	public HSSFWorkbook createExcel(String clazz, List<?> datas, String title,
			List<String> fieldsName) {
		CreateExcelImpl createExcel = new CreateExcelImpl(clazz, fieldsName);
		// 创建标题、表头、以及单元格数据
		try {
			createExcel.setTitle(title).setHead().setCellData(datas);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return createExcel.getWb();
	}


	/**
	 * 获取Excel
	 * 
	 * @param clazz
	 *            类名
	 * @param datas
	 *            数据
	 * @param title
	 *            标题
	 * @return
	 */
	public InputStream getExcelInputStream(String clazz, List<?> datas,
			String title, List<String> fieldsName) {

		ByteArrayOutputStream os = new ByteArrayOutputStream();

		try {
			createExcel(clazz, datas, title, fieldsName).write(os);
		} catch (IOException e) {
			e.printStackTrace();
		}
		InputStream is = new ByteArrayInputStream(os.toByteArray());

		return is;
	}

}
